package com.llkj.qingnianguoxue.login;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.ToastUtil;

/**
 * 注册
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener {
    private EditText etPhone, etCode, etPwd, etInviteCode;//手机，验证码，密码，邀请码
    private TextView tvGetCode, btnRegister;//获取验证码,注册
    private LinearLayout llAgree;//同意协议
    private TextView tvAgreemnet;//协议
    private TimeCount time;//倒计时
    private int durationTime = 60 * 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        setTitle("注册", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1, -1);

        initView();
        initListener();

        time = new TimeCount(durationTime, 1000);
    }

    private void initView() {
        etPhone = (EditText) findViewById(R.id.et_phone);
        etCode = (EditText) findViewById(R.id.et_code);
        etPwd = (EditText) findViewById(R.id.et_pwd);
        etInviteCode = (EditText) findViewById(R.id.et_friend_code);
        tvGetCode = (TextView) findViewById(R.id.tv_get_code);
        btnRegister = (TextView) findViewById(R.id.tv_register);
        llAgree = (LinearLayout) findViewById(R.id.ll_agree);
        tvAgreemnet = (TextView) findViewById(R.id.tv_agreement);
        llAgree.setSelected(true);
    }

    private void initListener() {
        tvGetCode.setOnClickListener(this);
        btnRegister.setOnClickListener(this);
        llAgree.setOnClickListener(this);
        tvAgreemnet.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String phone = null;
        switch (v.getId()) {
            case R.id.tv_get_code:
                phone = etPhone.getText().toString().trim();
                if (!StringUtils.isPhoneNumber(phone)) {
                    ToastUtil.makeLongText(this, "请输入正确的手机号格式");
                    return;
                }
                time.start();

                loadWaitDialog();
                HttpMethod.loginFormcode(this,this,phone,"1");
                break;
            case R.id.tv_register:
                phone = etPhone.getText().toString().trim();
                if (!StringUtils.isPhoneNumber(phone)) {
                    ToastUtil.makeLongText(this, "请输入正确的手机号格式");
                    return;
                }
                String code = etCode.getText().toString();
                if (StringUtils.isEmpty(code)) {
                    ToastUtil.makeLongText(this, "请输入验证码");
                    return;
                }
                String pwd = etPwd.getText().toString();
                if (pwd.length() < 6) {
                    ToastUtil.makeLongText(this, "请输入至少6位密码");
                    return;
                }
                if (!llAgree.isSelected()) {
                    ToastUtil.makeLongText(this, "请同意注册协议");
                    return;
                }
                String inviteCode = etInviteCode.getText().toString();

                loadWaitDialog();
                HttpMethod.loginRegister(this, this, phone, code, pwd, inviteCode);
                break;
            case R.id.ll_agree:
                if (llAgree.isSelected()) {
                    llAgree.setSelected(false);
                } else {
                    llAgree.setSelected(true);
                }
                break;
            case R.id.tv_agreement:
                Bundle bundle = new Bundle();
                bundle.putInt("from_class", 1);
                startActivity(AgreementActivity.class, bundle);
                break;
            case R.id.et_friend_code:
                break;
        }
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_LOGIN_REGISTER) {//注册
            ToastUtil.makeLongText(this,getString(R.string.register_success));
            finish();
        }else if(method == HttpMethod.HTTP_LOGIN_FORMCODE){//获取验证码
            ToastUtil.makeLongText(this,getString(R.string.send_code_success));
        }
    }

    @Override
    protected void onhttpFailed(String message, int method) {
        super.onhttpFailed(message, method);
        if(!StringUtils.isEmpty(message)){
            ToastUtil.makeLongText(this,message);
        }
    }

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {// 计时完毕
            if (tvGetCode != null) {
                tvGetCode.setText("重新发送");
                tvGetCode.setClickable(true);
            }
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
            if (tvGetCode != null) {
                tvGetCode.setClickable(false);//防止重复点击
                tvGetCode.setText(millisUntilFinished / 1000 + "s");
            }
        }
    }
}
